;*****************************************************************************************************
;+
; NAME:
;
;      eLOADCT (enhanced LOADCT)
;
; PURPOSE:
;
;      This is the enhanced version of the standard IDL LOADCT
;      command. It loads one of the predefined color tables into the
;      IGL internal color vectors. Note that, unlike the standard
;      LOADCT procedure, this change is immediately reflected on the current IGL
;      window. The routine relies on the cgLOADCT routine from the Coyote
;      Library. Also note that it does NOT change the IDL color vectors.
;
;
; CALLING SEQUENCE:
;
;      eLOADCT, ctindex
;
; ARGUMENTS:
;
;     CTINDEX:      Integer between 0 and 40 specifying the index of
;                   the color table to load.
;
;                 
; KEYWORD PARAMETERS:
;
;     _REF_EXTRA:   Any keyword accepted by the cgLOADCT
;                   procedure. See the Coyote Library documentation
;                   for more details.
;    
; PACKAGE:
;
;    Interactive Graphics Library (IGL)
;
; AUTHORS:
;
;        RAPHAEL SADOUN
;        Graduate Student
;        Institut d'Astrophysique de Paris
;        98 bis boulevard Arago
;        75014 PARIS FRANCE
;        E-mail: raphael.sadoun@gmail.com
;
;
; MODIFICATION HISTORY:
;
;     Written by: Raphael Sadoun, 24 April 2012
;-
;*****************************************************************************************************
PRO eLOADCT, ctindex, $
             RGB_TABLE=rgb_table, $
             _REF_EXTRA=extra

  COMPILE_OPT idl2, HIDDEN
  ON_ERROR, 2

  ;; Initialize the global system variables
  IGL_InitSysVars

  ;; Check positional arguments
  IF N_Elements(ctindex) EQ 0 THEN ctindex = 0
  IF (ctindex LT 0) OR (ctindex GT 40) THEN Message, 'Color table index must be between 0 and 40'

  ;; Call cgLOADCT with the RGB_TABLE keyword to get the color palette
  cgLOADCT, ctindex, rgb_table=palette, _EXTRA=extra

  ;; If RGB_TABLE is present, only returns the palette
  IF ARG_PRESENT(rgb_table) THEN BEGIN
     rgb_table = palette
     RETURN
  ENDIF

  ;; Load the palette into IGL private color table
  currentTable = IGL_GetSysVars('colorTable')
  currentTable -> SetProperty, ColorPalette=palette
  
  ;; IF there is a valid current IGL window, load the palette into its
  ;; internal color table
  currentWindow = IGL_GetCurrentWindow()

  IF OBJ_ISA_VALID(currentWindow,'IGL_Window') THEN BEGIN

     ;; Get the draw widget object
     drawWidget = currentWindow -> Get('DRAWWIDGET')
     
     IF OBJ_ISA_VALID(drawWidget,'DRAWWIDGET') THEN BEGIN

        ;; Get the IGL_ColorTable object associated with the window
        drawWidget -> GetProperty, Color_Object=colorTable

        ;; Load the new palette
        IF OBJ_ISA_VALID(colorTable,'IGL_ColorTable') THEN colorTable -> SetProperty, ColorPalette=palette, INDEX=ctindex
        
        ;; Get the name of the new color table
        LOADCT, GET_NAMES=tableNames, /silent
        name = strlowcase(tableNames[ctindex])     

        ;; Update the property panel to reflect the change of color table
        currentWindow -> SetPropertyAttribute, 'ColorTable', userdef=strlowcase(tableNames[ctindex])
        currentWindow -> GetProperty, ControlPanel=cp
        IF OBJ_VALID(cp) THEN cp -> Refresh_Properties, Properties='ColorTable'
        
     ENDIF

     ;; Refreshes window's content
     currentWindow -> DrawWindow, /BUFFERED_OUTPUT

  ENDIF
  
END
